package bgpv1

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
设备名称	邻居设备标识符	邻居AS域	启用vpn	启用路由反射
RR	10.10.10.2	100	否	否
AR2	30.30.30.30	20	否	否
RR	10.10.10.1	100	否	否
AR1	20.20.20.20	10	否	否
*/

type TableBGPv1Nei struct {
	DevName  string `json:"dev_name"`
	NeiId    string `json:"nei_id"`
	NeiAs    int    `json:"as"`
	Vpn      string `json:"vpn"`
	EnableRR string `json:"rr"`
}

// 将TableBGPv1Nei中的数据,写入到指定xlsx文件的neighbor表中
func WriteNeiXlsx(filename string, neis []TableBGPv1Nei) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("neighbor")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("neighbor", "A1", "设备名称")
	xlsx.SetCellValue("neighbor", "B1", "邻居设备标识符")
	xlsx.SetCellValue("neighbor", "C1", "邻居AS域")
	xlsx.SetCellValue("neighbor", "D1", "启用vpn")
	xlsx.SetCellValue("neighbor", "E1", "启用路由反射")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("dev", "A1", "G1", style)
	// 设置列宽
	// xlsx.SetColWidth("dev", "A", "G", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	fmt.Printf("len(neis): %v\n", len(neis))
	for i, nei := range neis {
		// fmt.Printf("nei: %v\n", nei)
		r := i + 2
		xlsx.SetCellValue("neighbor", fmt.Sprintf("A%v", r), nei.DevName)
		xlsx.SetCellValue("neighbor", fmt.Sprintf("B%v", r), nei.NeiId)
		xlsx.SetCellValue("neighbor", fmt.Sprintf("C%v", r), nei.NeiAs)
		xlsx.SetCellValue("neighbor", fmt.Sprintf("D%v", r), nei.Vpn)
		xlsx.SetCellValue("neighbor", fmt.Sprintf("E%v", r), nei.EnableRR)
	}
	xlsx.Save()
}
